home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP05.ZIP / CHAP05 / POLYLINE / POLYLINE.CPP < prev    next >
C/C++ Source or Header  |  1993-04-22  |  6KB  |  262 lines

  1. /*
  2.  * POLYLINE.CPP
  3.  * Modifications for Chapter 5.
  4.  *
  5.  * Implementation of the CPolyline class that we expose as an
  6.  * OLE 2.0 component object.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "polyline.h"
  19.  
  20.  
  21. /*
  22.  * CPolyline:CPolyline
  23.  * CPolyline::~CPolyline
  24.  *
  25.  * Constructor Parameters:
  26.  *  punkOuter       LPUNKNOWN of the controlling unknown.
  27.  *  pfnDestroy      LPFNDESTROYED to call when an object is destroyed.
  28.  *  hInst           HINSTANCE of the application we're in.
  29.  */
  30.  
  31. CPolyline::CPolyline(LPUNKNOWN punkOuter, LPFNDESTROYED pfnDestroy
  32.     , HINSTANCE hInst)
  33.     {
  34.     m_hWnd=NULL;
  35.     m_hInst=hInst;
  36.  
  37.     m_cRef=0;
  38.     m_punkOuter=punkOuter;
  39.     m_pfnDestroy=pfnDestroy;
  40.     m_fDirty=FALSE;
  41.  
  42.     //CHAPTER5MOD
  43.     m_pST=NULL;
  44.     m_cf =0;
  45.     m_clsID=CLSID_Polyline5;
  46.     m_iID  =IID_IPolyline5;
  47.  
  48.     //NULL any contained interfaces initially.
  49.     m_pIPolyline      =NULL;
  50.     m_pIPersistStorage=NULL;
  51.     m_pAdv            =NULL;
  52.  
  53.     //End CHAPTER5MOD
  54.     return;
  55.     }
  56.  
  57.  
  58. CPolyline::~CPolyline(void)
  59.     {
  60.     //CHAPTER5MOD
  61.     if (NULL!=m_pST)
  62.         delete m_pST;
  63.  
  64.     //Free our contained interfaces
  65.     if (NULL!=m_pIPersistStorage)
  66.         delete m_pIPersistStorage;
  67.     //End CHAPTER5MOD
  68.  
  69.     if (NULL!=m_pIPolyline)
  70.         delete m_pIPolyline;
  71.  
  72.     //Reverses the AddRef in ::SetAdvise
  73.     if (NULL!=m_pAdv)
  74.         m_pAdv->Release();
  75.  
  76.     return;
  77.     }
  78.  
  79.  
  80.  
  81.  
  82. /*
  83.  * CPolyline::FInit
  84.  *
  85.  * Purpose:
  86.  *  Performs any intiailization of a CPolyline that's prone to failure
  87.  *  that we also use internally before exposing the object outside
  88.  *  this DLL.
  89.  *
  90.  * Parameters:
  91.  *  None
  92.  *
  93.  * Return Value:
  94.  *  BOOL            TRUE if the function is successful, FALSE otherwise.
  95.  */
  96.  
  97. BOOL CPolyline::FInit(void)
  98.     {
  99.     LPUNKNOWN       pIUnknown=(LPUNKNOWN)this;
  100.  
  101.     if (NULL!=m_punkOuter)
  102.         pIUnknown=m_punkOuter;
  103.  
  104.     //CHAPTER5MOD
  105.     m_pST=new CStringTable(m_hInst);
  106.  
  107.     if (!m_pST->FInit(IDS_POLYLINEMIN, IDS_POLYLINEMAX))
  108.         return FALSE;
  109.  
  110.     m_cf=RegisterClipboardFormat(PSZ(IDS_STORAGEFORMAT));
  111.     //End CHAPTER5MOD
  112.  
  113.     //Allocate contained interfaces.
  114.     m_pIPolyline=new CImpIPolyline(this, pIUnknown);
  115.  
  116.     if (NULL==m_pIPolyline)
  117.         return FALSE;
  118.  
  119.     //CHAPTER5MOD
  120.     m_pIPersistStorage=new CImpIPersistStorage(this, pIUnknown);
  121.  
  122.     if (NULL==m_pIPersistStorage)
  123.         return FALSE;
  124.     //End CHAPTER5MOD
  125.  
  126.     return TRUE;
  127.     }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. /*
  136.  * CPolyline::QueryInterface
  137.  * CPolyline::AddRef
  138.  * CPolyline::Release
  139.  *
  140.  * Purpose:
  141.  *  IUnknown members for CPolyline object.
  142.  */
  143.  
  144. STDMETHODIMP CPolyline::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  145.     {
  146.     *ppv=NULL;
  147.  
  148.     /*
  149.      * The only calls we get here for IUnknown are either in a non-aggregated
  150.      * case or when we're created in an aggregation, so in either we always
  151.      * return our IUnknown for IID_IUnknown.
  152.      */
  153.     if (IsEqualIID(riid, IID_IUnknown))
  154.         *ppv=(LPVOID)this;
  155.  
  156.  
  157.     //For anything else we return contained interfaces.
  158.     if (IsEqualIID(riid, m_iID))
  159.         *ppv=(LPVOID)m_pIPolyline;
  160.  
  161.     //CHAPTER5MOD
  162.     if (IsEqualIID(riid, IID_IPersist) || IsEqualIID(riid, IID_IPersistStorage))
  163.         *ppv=(LPVOID)m_pIPersistStorage;
  164.     //End CHAPTER5MOD
  165.  
  166.     //AddRef any interface we'll return.
  167.     if (NULL!=*ppv)
  168.         {
  169.         ((LPUNKNOWN)*ppv)->AddRef();
  170.         return NOERROR;
  171.         }
  172.  
  173.     return ResultFromScode(E_NOINTERFACE);
  174.     }
  175.  
  176.  
  177. STDMETHODIMP_(ULONG) CPolyline::AddRef(void)
  178.     {
  179.     return ++m_cRef;
  180.     }
  181.  
  182.  
  183. STDMETHODIMP_(ULONG) CPolyline::Release(void)
  184.     {
  185.     ULONG       cRefT;
  186.  
  187.     cRefT=--m_cRef;
  188.  
  189.     if (0==m_cRef)
  190.         delete this;
  191.  
  192.     return cRefT;
  193.     }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. /*
  202.  * CPolyline::RectConvertMappings
  203.  *
  204.  * Purpose:
  205.  *  Converts the contents of a rectangle from device (MM_TEXT) or
  206.  *  HIMETRIC to the other.
  207.  *
  208.  * Parameters:
  209.  *  pRect           LPRECT containing the rectangle to convert.
  210.  *  fToDevice       BOOL TRUE to convert from HIMETRIC to device,
  211.  *                  FALSE to convert device to HIMETRIC.
  212.  *
  213.  * Return Value:
  214.  *  None
  215.  */
  216.  
  217. void CPolyline::RectConvertMappings(LPRECT pRect, BOOL fToDevice)
  218.     {
  219.     HDC      hDC;
  220.     int      iLpx, iLpy;
  221.  
  222.     if (NULL==pRect)
  223.         return;
  224.  
  225.     hDC=GetDC(NULL);
  226.     iLpx=GetDeviceCaps(hDC, LOGPIXELSX);
  227.     iLpy=GetDeviceCaps(hDC, LOGPIXELSY);
  228.     ReleaseDC(NULL, hDC);
  229.  
  230.     if (fToDevice)
  231.         {
  232.         pRect->left=MulDiv(iLpx, pRect->left, HIMETRIC_PER_INCH);
  233.         pRect->top =MulDiv(iLpy, pRect->top , HIMETRIC_PER_INCH);
  234.  
  235.         pRect->right =MulDiv(iLpx, pRect->right,  HIMETRIC_PER_INCH);
  236.         pRect->bottom=MulDiv(iLpy, pRect->bottom, HIMETRIC_PER_INCH);
  237.  
  238.        #ifdef NEVER
  239.         /*
  240.          * In this conversion we may get situations where the top
  241.          * coordinate is larger than the bottom, which messes us up.
  242.          */
  243.         if (pRect->bottom < pRect->top)
  244.             {
  245.             iLpy=pRect->top;
  246.             pRect->top=pRect->bottom;
  247.             pRect->bottom=iLpy;
  248.             }
  249.        #endif
  250.         }
  251.     else
  252.         {
  253.         pRect->left=MulDiv(pRect->left, HIMETRIC_PER_INCH, iLpx);
  254.         pRect->top =MulDiv(pRect->top , HIMETRIC_PER_INCH, iLpy);
  255.  
  256.         pRect->right =MulDiv(pRect->right,  HIMETRIC_PER_INCH, iLpx);
  257.         pRect->bottom=MulDiv(pRect->bottom, HIMETRIC_PER_INCH, iLpy);
  258.         }
  259.  
  260.     return;
  261.     }
  262.